﻿/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*  Author shang
*  2022-07-25 星期一 17:30
*/

using Autofac.Annotation;
using SqlSugar;
using TS.Common.Db;
using TS.Model;

namespace TS.Services;

[Component(AutofacScope = AutofacScope.InstancePerLifetimeScope)]
public class SshServerService : DbContext
{
    [Autowired] public ChilkatService _ChilkatService { get; set; }

    public async Task<HttpResult> AddOrUpdateSshServer(SshServer sshServer)
    {
        HttpResult res = new HttpResult();
        var count = 0;
        if (sshServer.ID == 0)
        {
            count = await Db.Insertable(sshServer).ExecuteCommandAsync();
        }
        else
        {
            count = await Db.Updateable(sshServer).ExecuteCommandAsync();
        }

        if (count == 0)
        {
            res.code = 1;
            res.msg = "新增修改失败！";
        }

        return res;
    }

    public async Task<HttpResult> GetList(SshServerSearchList searchList)
    {
        HttpResult res = new HttpResult();
        RefAsync<int> totalNumber = 0;
        var list = await Db.Queryable<SshServer>().WhereIF(searchList.name != null && searchList.name != "",
                x => x.ServerName.Contains(searchList.name))
            .ToPageListAsync(searchList.PageIndex, searchList.PageSize, totalNumber);
        PageList page = new PageList(totalNumber, list);
        res.data = page;
        return res;
    }

    public async Task<HttpResult> DeleteOne(SshServer sshServer)
    {
        HttpResult res = new HttpResult();
        var count = await Db.Deleteable<SshServer>().Where(x => x.ID == sshServer.ID).ExecuteCommandAsync();
        return res;
    }

    public async Task<HttpResult> GetOne(SshServer sshServer)
    {
        HttpResult res = new HttpResult();
        res.data = await Db.Queryable<SshServer>().Where(x => x.ID == sshServer.ID).FirstAsync();
        return res;
    }

    public async Task<HttpResult> ExecShell(int ID, string shell)
    {
        HttpResult res = new HttpResult();
        var sshServer = await Db.Queryable<SshServer>().Where(x => x.ID == ID).FirstAsync();
        var shellresult = _ChilkatService.SshShell(sshServer, shell);
        res.data = shellresult;
        return res;
    }

    public async Task<HttpResult> GetSelect()
    {
        HttpResult res = new HttpResult();
        res.data = await Db.Queryable<SshServer>().Select(x => new
        {
            label = x.ServerName,
            value = x.ID
        }).ToListAsync();

        return res;
    }
}